package logic

import (
	"context"
	"fmt"
	"zhihu/models"
	"zhihu/zhihu_rpc/internal/svc"
	"zhihu/zhihu_rpc/zhihu_rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type PushReplyLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPushReplyLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PushReplyLogic {
	return &PushReplyLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *PushReplyLogic) PushReply(in *zhihu_rpc.PushReplyRequest) (*zhihu_rpc.PushReplyResponse, error) {
	// todo: add your logic here and delete this line
	reply := models.Reply{
		UserId:    int(in.UserId),
		TroubleId: int(in.TroubleId),
		Content:   in.Des,
	}
	if models.DB.Create(&reply).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	log := models.UserLog{
		UserId: int(reply.UserId),
		Des:    "push his reply",
	}
	if models.DB.Create(&log).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	return &zhihu_rpc.PushReplyResponse{}, nil
}
